<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tool>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/employees/import')">excel导入</el-button>
          <el-button type="danger" size="small" @click="hExportExcel">excel导出</el-button>
          <el-button type="primary" size="small" @click="dialogVisible=true">新增员工</el-button>
        </template>
      </page-tool>

      <el-card style="margin-top: 10px;">
        <el-table border :data="data">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small" @click="hAssign(scope.row)">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-sizes="[1, 2, 3, 5]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 弹层组件 -->
    <el-dialog
      title="新增员工"
      :visible.sync="dialogVisible"
      width="40%"
      @close="hClose"
    >
      <emDialog ref="diaForm" @close="dialogVisible=false" />
    </el-dialog>
    <!-- 分配角色弹层 -->
    <el-dialog
      title="分配角色"
      :visible.sync="showVisible"
      width="40%"
    >
      <assignRoles v-if="showVisible" :employee-id="curId" @close="showVisible=false" />
    </el-dialog>
  </div>
</template>
<script>
import { getEmployees, delEmployee } from '@/api/employees'
import emDialog from './emDialog.vue'
import assignRoles from './assignRoles.vue'

export default {
  components: { emDialog, assignRoles },
  data() {
    return {
      data: [],
      page: 1,
      size: 5,
      total: 100,
      dialogVisible: false,
      showVisible: false,
      curId: ''
    }
  },
  created() {
    this.getEmployeesData()
  },

  methods: {
    // 当点击了分配角色
    hAssign(row) {
      this.showVisible = true
      this.curId = row.id
    },
    async getEmployeesData() {
      try {
        const res = await getEmployees(this.page, this.size)
        console.log(res)
        this.data = res.data.rows
        this.total = res.data.total
      } catch (error) {
        this.$message.error(error)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.size = val
      this.getEmployeesData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getEmployeesData()
    },
    hClose() {
      this.$refs.diaForm.resetForm()
    },

    async del(id) {
      try {
        const res = await delEmployee(id)
        console.log('delEmployee', res)
      } catch (err) {
        console.log('delEmployee', err)
      }
    },
    hDel(row) {
      this.$confirm('确认删除吗？').then(() => {
        this.del(row.id)
        if (this.data.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        // 重发请求
        this.getEmployeesData()
      }).catch((err) => {
        console.log(err)
      })
    },
    // 导出
    async hExportExcel() {
      const res = await getEmployees(this.page, this.size)
      console.log(res.data.rows)
      // 将英文的key转为中文的表头
      const { header, data } = this.formatData(res.data.rows)
      import('@/vendor/Export2Excel').then(excel => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    formatData(rows) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const header = Object.keys(rows[0]).map(item => map[item])
      const data = rows.map(obj => {
        return Object.values(obj)
      })
      return {
        header,
        data
      }
    }
  }
}

</script>
